import _instance from "./request";
import { JsonResult } from "@/model/utils.http.result";
import { param } from './index';

type Method =
  | "get"
  | "GET"
  | "delete"
  | "DELETE"
  | "head"
  | "HEAD"
  | "options"
  | "OPTIONS"
  | "post"
  | "POST"
  | "put"
  | "PUT"
  | "patch"
  | "PATCH"
  | "link"
  | "LINK"
  | "unlink"
  | "UNLINK";
async function request<T>(method: Method, url: string, data: unknown,params?:unknown) {

  const response = (await _instance.request({
    method,
    url,
    data,
    params
  })) as unknown;
  const result = response as JsonResult<T>;
  if (result.ec !== 200) return Promise.reject(result.data);
  return result.data;
}

export default {
  get<T>(url: string, query?: unknown) {
    return request<T>("get", url, undefined, query);
  },
  post<T>(url: string, data?: unknown) {
    return request<T>("post", url, data);
  },
  delete<T>(url: string, data?: unknown) {
    return request<T>("delete", url, data);
  },
  put<T>(url: string, data?: unknown) {
    return request<T>("put", url, data);
  },
  patch<T>(url: string, data?: unknown) {
    return request<T>("patch", url, data);
  }
};
